import grace from "../../../utils/grace.js";
import {Address} from '../address/address-model.js';

let address = new Address();

grace.page({

    /**
     * 页面的初始数据
     */
    data: {
        from_page: "",
        addressList: []
    },
    onLoad(e) {
        this.$data.from_page = e.from_page;
    },
    onShow() {
        let token = wx.getStorageSync('token');
        // token校验
        if (!token) {
            wx.removeStorageSync('userInfo');
            wx.navigateTo({
                url: '/pages/common/authorize/index'
            })
        } else {
            address.getAddressData({'token': wx.getStorageSync('token')}, (data) => {
                if (data.code == 0) {
                    this.$data.addressList = data.data;
                }
            })
        }
    },
    addAddress() {
        wx.navigateTo({
            url: '/pages/mall/address/new'
        })
    },
    wxAddress() {
        let that = this;
        wx.getSetting({
            success(res) {
                if (res.authSetting['scope.address']) {
                    wx.chooseAddress({
                        success(res) {
                            let params = {
                                'token': wx.getStorageSync('token'),
                                'parent_id': wx.getStorageSync('partner_id'),
                                'name': res.userName,
                                'mobile': res.telNumber,
                                'state': res.provinceName,
                                'city': res.cityName,
                                'street': res.detailInfo,
                                'default_address': true
                            };
                            address.addAddressData(params, (data) => {
                                if (data.code == 0) {
                                    if (that.$data.from_page == 'confirm') {
                                        that.$goBack({address_id: data.data.id});
                                    } else {
                                        that.$goBack();
                                    }
                                } else {
                                    wx.showToast({
                                        title: '添加失败',
                                        icon: 'loading',
                                    })
                                }
                            })
                        }
                    })
                } else {
                    if (res.authSetting['scope.address'] == false) {
                        wx.openSetting({
                            success(res) {
                                console.log(res.authSetting)
                            }
                        })
                    } else {
                        wx.chooseAddress({
                            success(res) {
                                let params = {
                                    'token': wx.getStorageSync('token'),
                                    'parent_id': wx.getStorageSync('partner_id'),
                                    'name': res.userName,
                                    'mobile': res.telNumber,
                                    'state': res.provinceName,
                                    'city': res.cityName,
                                    'street': res.detailInfo,
                                    'default_address': true
                                };
                                address.addAddressData(params, (data) => {
                                    if (data.code == 0) {
                                        if (that.$data.from_page == 'confirm') {
                                            that.$goBack({address_id: data.data.id});
                                        } else {
                                            that.$goBack();
                                        }
                                    } else {
                                        wx.showToast({
                                            title: '添加失败',
                                            icon: 'loading',
                                        })
                                    }
                                })
                            }
                        })
                    }
                }
            }
        })
    },
    deleteAddress(e) {
        address.deleteAddressData({
            'address_id': e.currentTarget.dataset.id,
            'token': wx.getStorageSync('token')
        }, (data) => {
            if (data.code == 0) {
                for (let i = 0; i < this.$data.addressList.length; i++) {
                    if (this.$data.addressList[i].id == e.currentTarget.dataset.id) {
                        this.$data.addressList.splice(i, 1);
                    }
                }
            }
        })
    },
    selectAddress(e) {
        if (this.$data.from_page == 'confirm') {
            this.$goBack({address_id: e.currentTarget.dataset.id});
        }
    }
})